package com.wsz.eduservice.controller;

import com.wsz.commonutils.R;
import com.wsz.eduservice.pojo.Chapter;
import com.wsz.eduservice.pojo.vo.chapter.ChapterVo;
import com.wsz.eduservice.service.ChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags="章节管理")
@RestController
@RequestMapping("/eduservice/chapter")
public class EduChapterController {
    @Autowired
    private ChapterService chapterService;

    @ApiOperation("章节列表")
    @GetMapping("/getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable(name = "courseId") String courseId){
        List<ChapterVo> list = new ArrayList<>();
        list = chapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("allChapterVideo",list);
    }

    @ApiOperation("增加章节")
    @PostMapping("/addChapter")
    public R addChapter(@ApiParam(name="Chapter",value="章节信息")
                            @RequestBody Chapter chapter){
        System.out.println(chapter);
       chapterService.save(chapter);
        return R.ok();
    }

    @ApiOperation("根据课程Id修改章节")
    @PostMapping("/updateChapter")
    public R updateChapter(@ApiParam(name="Chapter",value="章节信息")
                        @RequestBody Chapter chapter){
        chapterService.updateById(chapter);
        return R.ok();
    }

    @ApiOperation("根据章节Id获取章节")
    @GetMapping("/getChapterInfo/{chapterId}")
    public R getChapter(@ApiParam(name="ChapterId",value="课程ID")
                           @PathVariable String chapterId){
        Chapter byId = chapterService.getById(chapterId);
        return R.ok().data("chapter",byId);
    }

    @ApiOperation("根据章节Id删除章节和小节")
    @DeleteMapping("/deleteChapterAndVideo/{chapterId}")
    public R deleteChapterAndVideo(@ApiParam(name ="chapterId",value = "章节Id")
                               @PathVariable String chapterId) {
        chapterService.removeAllById(chapterId);
        return R.ok();
    }

    @ApiOperation("根据章节Id删除章节")
    @DeleteMapping("/deleteChapter/{chapterId}")
    public R deleteChapter(@ApiParam(name ="chapterId",value = "章节Id")
                           @PathVariable String chapterId) {
        Boolean bool = chapterService.deleteChapter(chapterId);
        if(bool) return R.ok();
        else return R.error();
    }
}
